package com.ewanghuiju.app.base;

import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.di.component.ActivityComponent;
import com.ewanghuiju.app.di.component.DaggerActivityComponent;
import com.ewanghuiju.app.di.module.ActivityModule;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.ToastUtil;
import com.muddzdev.styleabletoast.StyleableToast;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/6
 */
public abstract class BaseActivity<T extends BasePresenter> extends SimpleActivity implements BaseView {

    @Inject
    protected T mPresenter;

    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(App.getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        initInject();
        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        LoadingDialogUtils.dismissDialog_ios();
        if (mPresenter != null)
            mPresenter.detachView();
        super.onDestroy();
    }

    @Override
    public void showErrorMsg(String msg, boolean isShowToast) {
        LoadingDialogUtils.dismissDialog_ios();
        if (isShowToast)
            StyleableToast.makeText(mContext, msg, Toast.LENGTH_SHORT, R.style.mytoast).show();
    }

    @Override
    public void useNightMode(boolean isNight) {
    }

    @Override
    public void stateError() {

    }

    @Override
    public void stateEmpty() {

    }

    @Override
    public void stateLoading() {

    }

    @Override
    public void stateMain() {

    }

    /**
     * 短Toast
     *
     * @param msg 字符串消息内容
     */
    protected void toastShort(String msg) {
        ToastUtil.show(msg);
    }

    /**
     * 短Toast
     *
     * @param msg id
     */
    protected void toastShort(int msg) {
        toastShort(getString(msg));
    }


    protected abstract void initInject();

}
